package com.tsing.controller.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tsing.common.result.PageResult;
import com.tsing.common.result.Result;
import com.tsing.pojo.dto.NewsDTO;
import com.tsing.pojo.entity.TlPost;
import com.tsing.pojo.entity.WmNews;
import com.tsing.pojo.vo.PostDetailsVO;
import com.tsing.pojo.vo.WmNewsVO;
import com.tsing.service.NewsService;
import com.tsing.service.impl.NewsServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * @author 吴阳基
 * @datetime 2023/12/4 20:33
 */
public class NewsController {
    private final NewsService newsService = new NewsServiceImpl();

    public PageResult<PostDetailsVO> list(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer page = (Integer) jsonObject.get("page");
        page = (page != null && page > 0) ? page : 1;

        Integer size = (Integer) jsonObject.get("size");
        size = (size != null && size > 0) ? size : 10;

//        修改status为null
        Integer status = (Integer) jsonObject.get("status");
        status = status;

        String title = jsonObject.getString("title");
        title = (title != null && !title.isEmpty()) ? title : null;

        PageResult<PostDetailsVO> pageResult = newsService.list(page, size, status, title);

        return pageResult;
    }

    public Result pass(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer id = Integer.parseInt(jsonObject.getString("postId"));
        Integer status = Integer.parseInt(jsonObject.getString("status"));

        if (newsService.pass(id, status) > 0) {
            return Result.success();
        } else {
            return Result.error("失败");
        }
    }

    public Result fail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer id = Integer.parseInt(jsonObject.getString("postId"));
        Integer status = Integer.parseInt(jsonObject.getString("status"));
        String msg =  jsonObject.getString("msg");


        if (newsService.fail(id, status,msg) > 0) {
            return Result.success();
        } else {
            return Result.error("失败");
        }
    }

    public PageResult<PostDetailsVO> findPublished(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer page = (Integer) jsonObject.get("page");
        page = (page != null && page > 0) ? page : 1;

        Integer size = (Integer) jsonObject.get("size");
        size = (size != null && size > 0) ? size : 10;

        String title = jsonObject.getString("title");
        title = (title != null && !title.isEmpty()) ? title : null;

        Integer enable = (Integer) jsonObject.get("enable");
        enable = enable;

        Timestamp beginDate = jsonObject.getTimestamp("beginDate");
        beginDate = beginDate;

        Timestamp endDate = jsonObject.getTimestamp("endDate");
        endDate = endDate;

        NewsDTO newsDTO = new NewsDTO(page, size, title, enable, beginDate, endDate);

        return newsService.findPublished(newsDTO);
    }

    public Result down_or_up(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer id = jsonObject.getInteger("postId");
        Integer enable = jsonObject.getInteger("enable");

        if (newsService.down_or_up(id, enable) > 0) {
            return Result.success();
        } else {
            return Result.error("失败");
        }
    }

    public Result<PostDetailsVO> one(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;utf-8");

        // 获取请求体数据
        BufferedReader reader = request.getReader();
        StringBuilder jsonRequest = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            jsonRequest.append(line);
        }

        JSONObject jsonObject = JSON.parseObject(jsonRequest.toString());

        Integer id = jsonObject.getInteger("id");
        PostDetailsVO postDetailsVO = newsService.one(id);

        return Result.success(postDetailsVO);
    }

}
